package controller.dictionary;

import controller.common.BaseController;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.dictionary.*;
import wt.entity.dictionary.*;
import wt.staticData.ShortData;
import wt.webPlugin.ZTree;

import java.util.List;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("dictionary/publicOpen")
public class DictionaryPublicOpenController extends BaseController {
    @Autowired
    CertificateTypeServices certificateTypeServices;
    @Autowired
    ItemTypeServices itemTypeServices;

    @Autowired
    FundSetServices fundSetServices;

    @Autowired
    LeaveReasonServices leaveReasonServices;

    @Autowired
    LockReasonServices lockReasonServices;

    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    AffiliateCompanyServices affiliateCompanyServices;


    //    $.sysPlugin.initSelect2($("#111"),{
//        url: 'dictionary/publicOpen/affiliateCompanyAll'
//    });

    @ResponseBody
    @RequestMapping("affiliateCompanyAll")
    public JSONArray affiliateCompanyAll() throws Exception {
        List<AffiliateCompany> affiliateCompanyList = affiliateCompanyServices.selectByExample(null);
        JSONArray jsonArray = new JSONArray();
        for (AffiliateCompany affiliateCompany : affiliateCompanyList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", affiliateCompany.getCode());
            jsonObject.put("text", affiliateCompany.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    //    $.sysPlugin.initSelect2($("#111"),{
//        url: 'dictionary/publicOpen/affiliateCompanyOnlyLive'
//    });
    @ResponseBody
    @RequestMapping("affiliateCompanyOnlyLive")
    public JSONArray affiliateCompanyOnlyLive() throws Exception {
        List<AffiliateCompany> affiliateCompanyList = affiliateCompanyServices.selectLiveAffiliateCompany();
        JSONArray jsonArray = new JSONArray();
        for (AffiliateCompany affiliateCompany : affiliateCompanyList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", affiliateCompany.getCode());
            jsonObject.put("text", affiliateCompany.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;

    }






    //    $.sysPlugin.initSelect2($("#itemId"),{
//        url: 'dictionary/publicOpen/certificateTypeAll'
//    });
    @ResponseBody
    @RequestMapping("certificateTypeAll")
    public JSONArray certificateTypeAll() throws Exception {
        List<CertificateType> certificateTypeList = certificateTypeServices.selectByExample(null);
        JSONArray jsonArray = new JSONArray();
        for (CertificateType certificateType : certificateTypeList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", certificateType.getId());
            jsonObject.put("text", certificateType.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @ResponseBody
    @RequestMapping("certificateTypeOnlyLive")
    public JSONArray certificateTypeOnlyLive() throws Exception {
        CertificateTypeExample certificateTypeExample = new CertificateTypeExample();
        certificateTypeExample.createCriteria().andLiveEqualTo(ShortData.s1);
        List<CertificateType> certificateTypeList = certificateTypeServices.selectByExample(certificateTypeExample);
        JSONArray jsonArray = new JSONArray();
        for (CertificateType certificateType : certificateTypeList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", certificateType.getId());
            jsonObject.put("text", certificateType.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }


    @ResponseBody
    @RequestMapping("leaveReason")
    public JSONArray leaveReason() throws Exception {
        List<LeaveReason> leaveReasonList = leaveReasonServices.selectByExample(null);
        JSONArray jsonArray = new JSONArray();
        for (LeaveReason leaveReason : leaveReasonList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", leaveReason.getName());
            jsonObject.put("text", leaveReason.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    //    $.sysPlugin.initSelect2($("#itemId"),{
//        url: 'dictionary/publicOpen/lockReason'
//    });
    @ResponseBody
    @RequestMapping("lockReason")
    public JSONArray lockReason() throws Exception {
        List<LockReason> lockReasonList = lockReasonServices.selectByExample(null);
        JSONArray jsonArray = new JSONArray();
        for (LockReason lockReason : lockReasonList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", lockReason.getName());
            jsonObject.put("text", lockReason.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }


    @ResponseBody
    @RequestMapping("itemTypeAll")
    public JSONArray itemTypeAll() throws Exception {
        List<ItemType> itemTypeList = itemTypeServices.selectByExample(null);
        JSONArray jsonArray = new JSONArray();
        for (ItemType itemType : itemTypeList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", itemType.getId());
            jsonObject.put("text", itemType.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @ResponseBody
    @RequestMapping("itemTypeOnlyLive")
    public JSONArray itemTypeOnlyLive() throws Exception {
        List<ItemType> itemTypeList = itemTypeServices.selectLiveItemType();
        JSONArray jsonArray = new JSONArray();
        for (ItemType itemType : itemTypeList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", itemType.getId());
            jsonObject.put("text", itemType.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
    //    $.sysPlugin.initSelect2($("#itemId"),{
//        url: 'dictionary/publicOpen/itemTypeOnlyLive'
//    });
    @ResponseBody
    @RequestMapping("majorType")
    public JSONArray select(String itemId ,Boolean isOnlyLive) throws Exception {
        if (StringUtils.isBlank(itemId)){
            itemId=null;
        }
        if (isOnlyLive==null){
            isOnlyLive=true;
        }
        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(itemId, isOnlyLive);
        return ZTree.majorType2ZTreeJSONArray(majorTypeList, false);

    }


}
